package com.dapeis.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.common.example.oper.OperBuilder;
import tk.mybatis.mapper.entity.Example;

import com.ag.dao.DBService;
import com.ag.dao.IDao;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.dao.ICollocationExtMapper;
import com.dapeis.dao.ICollocationMapper;
import com.dapeis.dao.ICollocationSupplyExtMapper;
import com.dapeis.domain.Collocation;
import com.dapeis.exception.user.UserStateDisableException;

/**
 * @author 作者 E-mail:
 * @version 创建时间：2017年4月20日 上午11:09:00 类说明
 */
@Service
public class CollocationService extends DBService<Collocation> {

    @Resource
    ICollocationMapper mapper;

    @Resource
    ICollocationExtMapper CollocationExtMapper;

    @Resource
    ICollocationSupplyExtMapper supplyExtMapper;

    @Override
    protected IDao<Collocation> getIDao() {
	return mapper;
    }

    /**
     * 根据ID查找状态
     * 
     * @param collocationId
     */
    public void checkCollocationVaild(Integer collocationId) {
	this.checkCollocationVaild(getCollocation(collocationId));
    }

    /**
     * 检查状态
     * 
     * @param collocation
     */
    public void checkCollocationVaild(Collocation collocation) {
	if (collocation.getState() == StylistConstants.State.DISABLE) {
	    throw new UserStateDisableException();
	}
    }

    /**
     * 根据主键查找
     * 
     * @param collocationId
     * @return
     */
    public Collocation getCollocation(Integer collocationId) {
	return selectByPrimaryKey(collocationId);
    }

    /**
     * 根据手机号查找账号
     * 
     * @param contact
     * @return
     */
    public Collocation getCollocationByContact(String contact) {
	Example example = example();
	example.createCriteria().andEqualTo("contact", contact);
	return selectOneByExample(example);
    }

    /**
     * 更新登录记录
     * 
     * @param collocation
     * @param ip
     * @param deviceType
     * @param deviceName
     * @param appVersion
     * @return
     */
    public boolean doLoginRecord(Collocation collocation, String ip,
	    String deviceType, String deviceName, String appVersion) {
	OperBuilder operBuilder = new OperBuilder(Collocation.class);
	operBuilder.eq("loginIp", ip)
		.eq("lastLoginIp", collocation.getLoginIp())
		.eq("loginTime", System.currentTimeMillis() / 1000)
		.eq("lastLoginTime", collocation.getLoginTime())
		.eq("deviceType", deviceType).eq("deviceName", deviceName)
		.eq("appVersion", appVersion);
	Example example = example();
	example.createCriteria().andEqualTo("id", collocation.getId());
	return updateCustomOperByExample(operBuilder.get(), example) > 0;
    }

    public List<Map<String, Object>> queryDeviceTypeForIds(
	    List<Integer> collocationIds) {

	return mapper.queryCollocationListDeviceType(collocationIds);
    }

    public ICollocationExtMapper getCollocationExtMapper() {
	return CollocationExtMapper;
    }

    public ICollocationSupplyExtMapper getSupplyExtMapper() {
	return supplyExtMapper;
    }

    public void setSupplyExtMapper(ICollocationSupplyExtMapper supplyExtMapper) {
	this.supplyExtMapper = supplyExtMapper;
    }

}
